﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ITMAN.AssetMan_FRMs
{
    public partial class FRM_asset_wartung : Form
    {
        public FRM_asset_wartung()
        {
            InitializeComponent();
        }

        public static string asset_nr;

        private void FRM_asset_wartung_Load(object sender, EventArgs e)
        {
            Klassen.CL_assetman assetman = new Klassen.CL_assetman();
            DGV_wartung_historie.DataSource = assetman.get_asset_wartung_datatable(asset_nr);
            List<string> lst = assetman.get_asset_wartung_properties();
            DGV_neue_wartung.ColumnCount = lst.Count;
            DGV_neue_wartung.Rows.Add();
            for (int i = 0; i < lst.Count; i++)
            {
                DGV_neue_wartung.Columns[i].Name = lst[i];
                switch (lst[i])
                {
                    case "WID":
                        DGV_neue_wartung.Rows[0].Cells[i].ReadOnly = true;
                        break;
                    case "Fehler_code":
                        DataGridViewComboBoxCell combo_cell = new DataGridViewComboBoxCell();
                        combo_cell.AutoComplete = true;
                        List<string> lst_code = assetman.get_wartung_fehlercode();
                        for (int ii = 0; ii < lst_code.Count; ii++)
                        {
                            combo_cell.Items.Add(lst_code[ii]);
                        }
                        DGV_neue_wartung[i, 0] = combo_cell;
                        break;
                    case "Ersteller":
                        DGV_neue_wartung.Rows[0].Cells[i].ReadOnly = true;
                        break;
                    case "Erstelldatum":
                        DGV_neue_wartung.Rows[0].Cells[i].ReadOnly = true;
                        break;
                }

            }

        }

        private void DGV_neue_wartung_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                DGV_neue_wartung.Rows[0].Cells[1].Value = asset_nr;
                DGV_neue_wartung.Rows[0].Cells[1].ReadOnly = true;
            }
        }

        private void BTN_save_wartung_Click(object sender, EventArgs e)
        {
            try
            {
                DGV_neue_wartung.Rows[0].Selected = true;
                Klassen.CL_assetman assetman = new Klassen.CL_assetman();
                DataGridViewRow row = DGV_neue_wartung.SelectedRows[0];
                assetman.insert_new_asset_wartung(row);
                DGV_neue_wartung.Rows.Clear();
                MessageBox.Show("Wartung wurde eingetragen");
                this.Refresh();
            }
            catch (SystemException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


    }
}
